package com.senseu.pages;

import com.senseu.base.BasePage;
import io.appium.java_client.AppiumBy;
import io.appium.java_client.AppiumDriver;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Pause;
import org.openqa.selenium.interactions.PointerInput;
import org.openqa.selenium.interactions.Sequence;

import java.time.Duration;
import java.util.Arrays;
import java.util.List;

public class SelectDevice2BindParentPage extends BasePage {

    public SelectDevice2BindParentPage(AppiumDriver driver) {
        super(driver);
    }

    public SelectDevice2BindSonPage clickBaby() {
        // 默认进入sub界面
        logger.info("查找Sense-U Baby");
//        WebElement baby = driver.findElement(AppiumBy.androidUIAutomator("new UiSelector().text(\"Sense-U Baby\")"));
        WebElement baby = driver.findElement(AppiumBy.xpath("(//android.widget.LinearLayout[@resource-id=\"com.senseu.baby:id/llDevice\"])[1]"));

        // 点击和元素无关，防止动画刷新导致报错
        Point tapPoint = baby.getLocation();
        logger.info("点击Sense-U Baby " + tapPoint.toString());
        var finger = new PointerInput(PointerInput.Kind.TOUCH, "finger");
        var tap = new Sequence(finger, 1);
        tap.addAction(finger.createPointerMove(Duration.ofMillis(0),
                PointerInput.Origin.viewport(), tapPoint.x, tapPoint.y));
        tap.addAction(finger.createPointerDown(PointerInput.MouseButton.LEFT.asArg()));
        tap.addAction(new Pause(finger, Duration.ofMillis(50)));
        tap.addAction(finger.createPointerUp(PointerInput.MouseButton.LEFT.asArg()));
        driver.perform(Arrays.asList(tap));
        return new SelectDevice2BindSonPage(driver);
    }
}
